﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_pricesetrule_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();
            bindPriceList();

        }

        private void bindPriceList()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlPriceList.DataSource = dal.GetPriceType(int.Parse(this.ddlComList.SelectedValue), null, false);
            this.ddlPriceList.DataBind();
        }

        protected void ddlComList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindPriceList();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_face_m_unit_priceset_rule(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetPriceSetRule(int.Parse(this.hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        this.ddlComList.ClearSelection(); this.ddlPriceList.ClearSelection();
                        this.ddlComList.Items.FindByValue(dt.Rows[0]["p_self_community_code"].ToString()).Selected = true;
                        bindPriceList();
                        this.ddlPriceList.Items.FindByValue(dt.Rows[0]["price_code"].ToString()).Selected = true;
                        this.txtPriceName.Text = dt.Rows[0]["price_name"].ToString();
                        this.ddlDependOn.Items.FindByValue(dt.Rows[0]["dependon_field"].ToString()).Selected = true;
                        this.ddlFlagField.Items.FindByValue(dt.Rows[0]["flag_field"].ToString()).Selected = true;
                        this.ddlPriceRule.Items.FindByValue(dt.Rows[0]["price_rule"].ToString()).Selected = true;
                        this.txtFromValue.Text = dt.Rows[0]["from_value"].ToString();
                        this.txtUntilValue.Text = dt.Rows[0]["until_value"].ToString();
                        this.txtSetPrice.Text = dt.Rows[0]["setprice"].ToString();
                    }
                    catch
                    { }
                }
            }
        }


        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sCommunity = this.ddlComList.SelectedValue;
            string sPriceCode = this.ddlPriceList.SelectedValue;
            string sPriceName = this.txtPriceName.Text.Trim();
            string sDependOn = this.ddlDependOn.SelectedValue;
            string sFlagField = this.ddlFlagField.SelectedValue;
            string sPriceRule = this.ddlPriceRule.SelectedValue;
            string sFromValue = this.txtFromValue.Text.Trim();
            string sUntilValue = this.txtUntilValue.Text.Trim();
            string sSetPrice = this.txtSetPrice.Text.Trim();

            if (sPriceCode == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "所属费类不能为空！");
                return;
            }
            if (sPriceName == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "费项名称不能为空！");
                return;
            }
            decimal dFrom = 0;decimal dUntil = 0;decimal dSetPrice = 0;
            if (sFromValue != string.Empty) { if (!decimal.TryParse(sFromValue, out dFrom)) { PM.Web.Common.JsAlert(this, "极小值请输入数字！"); return; } }
            if (sUntilValue != string.Empty) { if (!decimal.TryParse(sUntilValue, out dUntil)) { PM.Web.Common.JsAlert(this, "极大值请输入数字！"); return; } }
            if (sSetPrice != string.Empty) { if (!decimal.TryParse(sSetPrice, out dSetPrice)) { PM.Web.Common.JsAlert(this, "单价请输入数字！"); return; } }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_unit_priceset_rule(int.Parse(sCommunity),int.Parse(sPriceCode),sPriceName,"","",false,sPriceRule,sDependOn,sFlagField,dFrom,dUntil,dSetPrice))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.Modify_face_m_unit_priceset_rule(int.Parse(this.hidId.Value), int.Parse(sCommunity), int.Parse(sPriceCode), sPriceName, "", "", false, sPriceRule, sDependOn, sFlagField, dFrom, dUntil, dSetPrice))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }

        
    }
}